<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 16/3/29
 * Time: 上午11:12
 */
class OdsCustomController extends BaseController{
    /*
     * 采集用户车辆信息
     * */
    /**
     * @Author:      lyt
     * @DateTime:    2016-04-28 11:21:37
     * @Description: 修改逻辑  去除 车辆认证  修改为认证 修改的修改认证  不修改 车辆 里程  改为修改订单里程 
     * 待 订单 结束 时候修改订单里程 修改订单的 车辆信息 
     */
    public function update_car_info(){
        $validator = Validator::make(Input::all(),
            array(
                'order_id' => 'required',
                'token' => 'required',
            )
        );
        $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("ods_error.args_miss_error_12001");
                break;
            }

            //传入参数
            $order_id = Input::get('order_id');
            $token = Input::get('token');
            $car_model = Input::get('car_model')?Input::get('car_model'):0;
            $car_no = Input::get('car_no')?Input::get('car_no'):'';
            $vin = Input::get('vin')?Input::get('vin'):'';
            $vin = strtoupper($vin);
            $car_no = strtoupper($car_no);
            $mileage = Input::get('mileage')?Input::get('mileage'):0;

            //验证token
            $car_data = MaintenanceCars::getMaintenanceCarByToken($token);
            if(is_numeric($car_data) && $car_data < 0){
                $response = Lang::get("ods_error.token_check_error_12005");
                break;
            }

            //获取用户信息
            $user_car = Orders::where('order_id',$order_id)->select(array('user_id','car_no'))->first();
            try{
                //修改车辆信息
                $user_car_info = UserCarInfo::firstOrNew(array('user_id' => $user_car->user_id ,'car_no' => $user_car->car_no,"status"=> 1));
                if($car_model != 0) {
                    $csi=CarStandardInfo::find($car_model);
                    $user_car_info->car_series_id = $csi->series_name_id;
                    $user_car_info->car_brand_id = $csi->brand_name_id;
                    $user_car_info->car_model_id = $csi->id;
                }
                if($car_no != '') $user_car_info->car_no = $car_no;
                if($vin != '') $user_car_info->vin_code = $vin;
                // if($mileage != 0) $user_car_info->mileage = $mileage;
                $user_car_info->is_lock = 1;
                $user_car_info->save();
                //修改订单的里程
                $csi=CarStandardInfo::find($user_car_info->car_model_id);
                $order=Orders::find($order_id);
                $order->mileage = $mileage;
                $order->car_brand_id = $csi->brand_name_id;
                $order->car_brand_name = $csi->brand_name;
                $order->car_series_id = $csi->series_name_id;
                $order->car_series_name = $csi->series_name;
                $order->car_model_id = $csi->id;
                $order->car_model_name = $csi->model_name;
                $order->save();
                //修改 认证车辆 
                if($user_car_info->is_lock==1){//如果是认证车辆
                    //修改其他车型不同的的认证车辆 认证状态为未认证
                    UserCarInfo::where("car_no",$user_car_info->car_no)->where("is_lock",1)->where("car_model_id","!=",$user_car_info->car_model_id)->update(array("is_lock"=>0));
                    UserCarInfo::where("id",$user_car_info->id)->update(array("is_lock"=>1));
                    //修改认证车型信息
                    // $uci->is_lock=1;
                    // $uci->save();
                    $uacs=UserAuthenticationCar::where('car_no',$user_car_info->car_no)->where('car_model_id',$user_car_info->car_model_id)->where('status',1)->get();
                    if(!sizeof($uacs)){                        
                        UserAuthenticationCar::where("car_no",$user_car_info->car_no)->update(array("status"=>0));
                        $uac=new UserAuthenticationCar();
                        $uac->status=1;
                        $uac->car_no=$user_car_info->car_no;
                        $uac->vin_code=$vin;
                        $uac->mileage=$user_car_info->mileage;
                        $uac->car_brand_id=$csi->brand_name_id;
                        $uac->car_series_id=$csi->series_name_id;
                        $uac->car_model_id=$csi->id;
                        $uac->shop_id=$sc_user->shop_id;
                        $uac->user_id=$user_car_info->user_id;
                        $uac->create_by=0;
                        $uac->save();
                    }
                }    

            } catch (Exception $e) {
                Log::error("error", array("system" => "ods" ,"event" => "update_car_info", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("ods_error.save_data_error_12014");
                break;
            }

            //组织返回
            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => array(
                    'order_id' => $order_id,
                    'car_no' => $car_no
                )
            );
        } while (false);
        Log::info("info", array("system" => "ods" ,"event" => "update_car_info", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}
